//
//  CoreDataManager.m
//  Lesson_5_记事本
//
//  Created by 大展 on 16/5/31.
//  Copyright © 2016年 Mario. All rights reserved.
//

#import "CoreDataManager.h"

@interface CoreDataManager()

@property (nonatomic, strong)NSManagedObjectContext *context;

@end

static CoreDataManager *manager = nil;
@implementation CoreDataManager

// 懒加载
- (NSManagedObjectContext *)context {

    if (!_context) {

        _context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
    }
    return _context;
}

// 单例
+ (instancetype)defaultManager {

    if (!manager) {

        manager = [[CoreDataManager alloc]init];
    }
    return manager;
}

// 重写init方法 实现coredata的基本配置
- (instancetype)init
{
    self = [super init];
    if (self) {

        NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]];
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"read.sqlite"];
        NSLog(@"%@",filePath);
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:nil error:nil];
        self.context.persistentStoreCoordinator = store;
    }
    return self;
}

// 查询整个收藏
- (NSArray *)selectAllCollectionModel{

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CollectionModel"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    NSLog(@"%ld",array.count);
    return array;
}
// 查看有有没有已经存在的阅读收藏
- (BOOL)isContainsReadWithReadID:(NSNumber *)readID {

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CollectionModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"readID = %@",readID];
    request.predicate = predicate;
    NSArray *array = [self.context executeFetchRequest:request error:nil];

    if (array.count != 0) {

        return YES;
    } else {

        return NO;
    }
}

// 根据readID去删除一个收藏
- (void)deleteCollectionModelWithReadID:(NSNumber *)readID {

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CollectionModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"readID = %@",readID];
    request.predicate = predicate;

    CollectionModel *collectionModel = [self.context executeFetchRequest:request error:nil].lastObject;

    [self.context deleteObject:collectionModel];
    [self.context save:nil];
}

// 插入
- (void)insertCollectionModelWithReadModel:(ReadListModel *)readModel html:(NSString *)html {

    if ([self isContainsReadWithReadID:readModel.ID]) {
        // 表示已经收藏
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经收藏了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [alertView show];
        return;
    }
    CollectionModel *collectionModel = [NSEntityDescription insertNewObjectForEntityForName:@"CollectionModel" inManagedObjectContext:self.context];

    collectionModel.title = readModel.title;
    collectionModel.readID = [NSString stringWithFormat:@"%@",readModel.ID];
    collectionModel.coverimg = readModel.coverimg;
    collectionModel.name = readModel.name;
    collectionModel.html = html;

    [self.context save:nil];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

    [alertView show];
}

- (void)insertDownloadModelWithUrl:(NSString *)url filePath:(NSString *)filePath title:(NSString *)title {

    if ([self isContainDownloadModelWithUrl:url]) {
//        // 表示已经收藏
//        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经收藏了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//
//        [alertView show];
        return;
    }
    DownloadModel *downloadModel = [NSEntityDescription insertNewObjectForEntityForName:@"DownloadModel" inManagedObjectContext:self.context];

    downloadModel.musicUrl = url;
    downloadModel.filePath = filePath;
    downloadModel.title = title;

    [self.context save:nil];
//    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

//    [alertView show];

}

- (BOOL)isContainDownloadModelWithUrl:(NSString *)url {

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"DownloadModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"musicUrl = %@",url];
    request.predicate = predicate;
    NSArray *array = [self.context executeFetchRequest:request error:nil];

    if (array.count != 0) {

        return YES;
    } else {

        return NO;
    }
}

- (NSArray *)selectAllDownloadModel{

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"DownloadModel"];
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    NSLog(@"%ld",array.count);
    return array;
}


@end
